package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.common.exception.LogicalException;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService{

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Override
    public UserInfo get(Long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean checkPhone(String phone) {
        int count = userInfoMapper.selectCountByPhone(phone);
        return count > 0;
    }

    @Override
    public void sendVerifyCode(String phone) {
        //1. 创建验证码
        String code = UUID.randomUUID().toString().replaceAll("-", "")
                .substring(0, 4);
        //2. 拼接短信信息
        StringBuilder builder = new StringBuilder(50);
        builder.append("您注册的短信验证码为:").append(code).append(", ").append(Consts.VERIFY_CODE_VAI_TIME)
                .append("分钟内有效, 请勿泄露.");
        //3. 发送短信
        System.out.println(builder);
        //4. 将验证码和缓存到Redis中
        userInfoRedisService.setVerifyCode(phone, code);
    }

    //用户注册
    @Override
    public void userRegist(String phone, String nickname, String password,
                           String rpassword, String verifyCode) throws LogicalException{
        //1. 判断各个参数是否为空
        AssertUtil.hasLength(phone,"手机号码不能为空");
        AssertUtil.hasLength(nickname,"昵称不能为空");
        AssertUtil.hasLength(password,"密码不能为空");
        AssertUtil.hasLength(rpassword,"确认密码不能为空");
        AssertUtil.hasLength(verifyCode,"验证码不能为空");
        //2. 判断两次输入的密码是否一致
        AssertUtil.isEquals(password, rpassword, "两次密码不一致");
        //3. 判断手机号码的格式是否正确
        //4. 判断手机号码是否唯一
        if (this.checkPhone(phone)) {
            throw new LogicalException("手机号已被注册");
        }
        //5. 短信验证码是否一致
        String code = userInfoRedisService.getVerifyCode(phone);
        if (code ==null || !code.equalsIgnoreCase(verifyCode)){
            throw new LogicalException("验证码错误或者失效");
        }
        //用户注册
        UserInfo user = new UserInfo();
        user.setHeadImgUrl("/images/default.png");
        user.setLevel(1);
        user.setNickname(nickname);
        //需要加密
        user.setPassword(password);
        user.setPhone(phone);
        user.setState(UserInfo.STATE_NORMAL);
        userInfoMapper.insert(user);
    }

    @Override
    public String userLogin(String username, String password) throws LogicalException{
        //1. 判断参数是否为空
        AssertUtil.hasLength(username,"用户名不能为空");
        AssertUtil.hasLength(password,"密码不能为空");
        //2. 判断用户是否为空
        UserInfo user = userInfoMapper.selectByUsernameAndPassword(username, password);
        if (user == null){
            throw new LogicalException("账号或密码错误");
        }
        //3. 登录成功之后, 定义一个变量token, 值为UUID的随机值
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        //4. 将生成的UUID作为Redis的key值, 登录的用户对象作为Redis的value值
        //缓存到Redis数据库中, 并设置key的有效时间为30分钟
        userInfoRedisService.setToken(token, user);
        return token;
    }

    @Override
    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }

    @Override
    public boolean update(UserInfo userInfo) {
        userInfoMapper.update(userInfo);
        return true;
    }
}
